namespace KinonekoSoftware.Studio.Components
{
    public sealed class MasterDetail
    {
        public string Name  { get; init; }
        public string Intro { get; init; }
    }
    
    public sealed class MasterDetailViewModel : ObjectDialog<MasterDetail>
    {
        private string       _intro;
        private string       _name;

        public static Task<Result<MasterDetail>> New(string title = null, string description = null)
        {
            var vm = new MasterDetailViewModel();
            vm.Title       = title;
            vm.Description = description;

            return Dialog<MasterDetail>(vm);
        }
        
        public static Task<Result<MasterDetail>> Edit(string name, string intro, string title = null, string description = null)
        {
            var vm      = new MasterDetailViewModel();
            var context = new DialogContext();
            
            vm.Title       = title;
            vm.Description = description;
            context.Set(ContextBase.Key_Value, name);
            context.Set(DialogContext.Key_Description, intro);

            return Dialog<MasterDetail>(vm, context);
        }

        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(
                      DialogRule.Create<MasterDetailViewModel>(
                                                               TextSR.Parameter_Empty_Value, 
                                                               x => !string.IsNullOrEmpty(x.Name)));
        }
        
        protected override bool OnReceive(DialogContext parameter)
        {
            if (parameter.Has(ContextBase.Key_Value))
            {
                Name = parameter.GetString(ContextBase.Key_Value);
            }

            if (parameter.Has(DialogContext.Key_Description))
            {
                Intro =parameter.GetString(DialogContext.Key_Description);
            }
            
            return false;
        }

        protected override MasterDetail OnFinish(bool editing)
        {
            return new MasterDetail
            {
                Name  = Name,
                Intro = Intro,
            };
        }

        /// <summary>
        /// 获取或设置 <see cref="Name"/> 属性。
        /// </summary>
        public string Name
        {
            get => _name;
            set => TryFinishAndSetValue(ref _name, value);
        }
        /// <summary>
        /// 获取或设置 <see cref="Intro"/> 属性。
        /// </summary>
        public string Intro
        {
            get => _intro;
            set => SetValue(ref _intro, value);
        }
    }
}